package com.xh.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.scripting.support.ResourceScriptSource;

@Configuration
public class RedisLuaScriptConfig {

    @Bean(name = "redisOrderStock")
    public DefaultRedisScript<Long> redisOrderStock() {
        DefaultRedisScript<Long> longDefaultRedisScript = new DefaultRedisScript<>();
        // 设置返回值
        longDefaultRedisScript.setResultType(Long.class);
        // 加载lau脚本
        longDefaultRedisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("script/lua/redis-order-stock.lua")));
        return longDefaultRedisScript;
    }

    @Bean(name = "redisOrderStockV2")
    public DefaultRedisScript<Long> redisOrderStockV2() {
        DefaultRedisScript<Long> longDefaultRedisScript = new DefaultRedisScript<>();
        longDefaultRedisScript.setResultType(Long.class);
        longDefaultRedisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("script/lua/redis-order-stock-v2.lua")));
        return longDefaultRedisScript;
    }

}